package com.ops.service.ns.system.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSLookUps;
import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.OPSRuleRates;
import com.ops.schema.ns.system.OPSTaxAuthority;
import com.ops.schema.ns.system.SearchLookUpsRequest;
import com.ops.schema.ns.system.SearchLookUpsResponse;
import com.ops.schema.ns.system.SearchRuleRatesRequest;
import com.ops.schema.ns.system.SearchRuleRatesResponse;
import com.ops.schema.ns.system.SearchTaxAuthoritiesRequest;
import com.ops.schema.ns.system.SearchTaxAuthoritiesResponse;
import com.ops.schema.ns.system.UpdateRuleRatesRequest;
import com.ops.schema.ns.system.UpdateRuleRatesResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class RuleRateMBean {
	private String ruleName;
	private String taxAuthority;
	private String taxType;
	private String rateType;
	private String mode;
	private String delflag;
	private String saveStatus;
	private String tempParentFlag;
	private List<SelectItem> taxTypeList;
	private List<SelectItem> taxAuthorityList;
	private List<SelectItem> rateTypeList;
	private List<SelectItem> parentRateList;
	private List<OPSRuleRates> opsRatelst;
	private OPSRuleRates opsRuleRates;
	private Map<String, OPSRuleRates> opsRateMap;

	public void searchRuleRates() {
		System.out.println("Inside searchRules");
		SearchRuleRatesRequest ruleRatesRequest = new SearchRuleRatesRequest();
		ruleRatesRequest.setRuleName(ruleName);
		ruleRatesRequest.setRateType(rateType);
		ruleRatesRequest.setTaxAuthority(taxAuthority);
		ruleRatesRequest.setTaxType(taxType);
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		ruleRatesRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchRuleRatesResponse rulesResponse = OnlinePayrollSystemServiceClient
					.searchRuleRate(ruleRatesRequest);
			setOpsRatelst(rulesResponse.getOPSRuleRates());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetRuleRates() {
		setRuleName(null);
		setTaxAuthority(null);
		setTaxType(null);
		setRateType(null);

	}

	public void saveRuleRates(ActionEvent event) {

		UpdateRuleRatesRequest updateRequest = new UpdateRuleRatesRequest();
		OPSRuleRates opsRates = new OPSRuleRates();
		opsRates.setRateId(opsRuleRates.getRateId());
		opsRates.setRuleName(opsRuleRates.getRuleName());
		opsRates.setParentRateId(opsRuleRates.getParentRateId());
		opsRates.setTaxAuthority(opsRuleRates.getTaxAuthority());
		opsRates.setTaxType(opsRuleRates.getTaxType());
		opsRates.setRateType(opsRuleRates.getRateType());
		opsRates.setOperationFlag("U");
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		if(opsRates.getRateId() != null)
			uditInfo.setAction("U");
			else
			uditInfo.setAction("I");	
			uditInfo.setObjectType(getMenuId());
		opsRates.setOPSRecordAuditInfo(uditInfo);
		updateRequest.getOPSRuleRates().add(opsRates);
		try {
			UpdateRuleRatesResponse response = persistData(updateRequest);
			System.out.println("STATUS:"
					+ response.getServiceResult().getStatusMsg());
			setSaveStatus(response.getServiceResult().getStatusMsg());
			searchRuleRates();
		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void delRuleRates(ActionEvent event) {
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (mode.startsWith("Del_") && delflag != null
					&& delflag.equalsIgnoreCase("Yes")) {
				setDelflag(null);
				UpdateRuleRatesRequest updateRequest = new UpdateRuleRatesRequest();
				String key = mode.replace("Del_", "").trim();
				System.out.println("RateId:::"+key);
				OPSRuleRates rates = new OPSRuleRates();
				rates.setRateId(key);
				rates.setOperationFlag("D");
				OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
				uditInfo.setUpdatedBy(getuserId());
				uditInfo.setUpdatedSession(getSessionId());
				uditInfo.setAction("D");	
				uditInfo.setObjectType(getMenuId());
				rates.setOPSRecordAuditInfo(uditInfo);
				updateRequest.getOPSRuleRates().add(rates);
				try {
					persistData(updateRequest);
					searchRuleRates();
				} catch (ServiceException e) {
					e.printStackTrace();
				}

			}
		}

	}

	public OPSRuleRates addRuleRates() {
		setSaveStatus(null);
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (getOpsRatelst() != null) {
				opsRateMap = new HashMap<String, OPSRuleRates>();
				for (OPSRuleRates rates : getOpsRatelst()) {
					opsRateMap.put(rates.getRateId(), rates);
				}
			}
			if (mode.equalsIgnoreCase("newAdd")) {
				opsRuleRates = new OPSRuleRates();
				setTempParentFlag(null);
				setParentRateList(null);
				getParentRateList();
			} else if (mode.startsWith("Edit_")) {
				String key = mode.replace("Edit_", "").trim();
				System.out.println("Key:" + key);
				setTempParentFlag(key);
				setParentRateList(null);
				getParentRateList();
				opsRuleRates = opsRateMap.get(key);
			}
		}
		System.out.println("Inside addRules:::" + mode);
		return opsRuleRates;
	}

	public UpdateRuleRatesResponse persistData(
			UpdateRuleRatesRequest ratesRequest) throws ServiceException {
		UpdateRuleRatesResponse ratesResponse = OnlinePayrollSystemServiceClient
				.saveRuleRate(ratesRequest);
		return ratesResponse;
	}

	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext()
				.getSession(false);
		return session.getId();
	}

	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	public String getRuleName() {
		return ruleName;
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}

	public String getTaxAuthority() {
		return taxAuthority;
	}

	public void setTaxAuthority(String taxAuthority) {
		this.taxAuthority = taxAuthority;
	}

	public String getTaxType() {
		return taxType;
	}

	public void setTaxType(String taxType) {
		this.taxType = taxType;
	}

	public String getRateType() {
		return rateType;
	}

	public void setRateType(String rateType) {
		this.rateType = rateType;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getDelflag() {
		return delflag;
	}

	public void setDelflag(String delflag) {
		this.delflag = delflag;
	}

	public String getSaveStatus() {
		return saveStatus;
	}

	public void setSaveStatus(String saveStatus) {
		this.saveStatus = saveStatus;
	}

	public List<OPSRuleRates> getOpsRatelst() {
		return opsRatelst;
	}

	public void setOpsRatelst(List<OPSRuleRates> opsRatelst) {
		this.opsRatelst = opsRatelst;
	}

	public OPSRuleRates getOpsRuleRates() {
		return opsRuleRates;
	}

	public void setOpsRuleRates(OPSRuleRates opsRuleRates) {
		this.opsRuleRates = opsRuleRates;
	}

	public Map<String, OPSRuleRates> getOpsRateMap() {
		return opsRateMap;
	}

	public void setOpsRateMap(Map<String, OPSRuleRates> opsRateMap) {
		this.opsRateMap = opsRateMap;
	}

	public List<SelectItem> getTaxTypeList() {
		if (taxTypeList == null) {
			taxTypeList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			taxTypeList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("TAX-TYPE");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					taxTypeList.add(item);
				}
				System.out.println("taxTypeList:" + taxTypeList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return taxTypeList;
	}

	public void setTaxTypeList(List<SelectItem> taxTypeList) {
		this.taxTypeList = taxTypeList;
	}

	public List<SelectItem> getTaxAuthorityList() {
		if (taxAuthorityList == null) {
			taxAuthorityList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			taxAuthorityList.add(defitem);
			SearchTaxAuthoritiesRequest masterRequest = new SearchTaxAuthoritiesRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			try {
				SearchTaxAuthoritiesResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchTaxAuthorities(masterRequest);
				for (OPSTaxAuthority master : masterResponse.getOPSTaxAuthority()) {
					SelectItem item = new SelectItem(master.getId(),
							master.getName());
					taxAuthorityList.add(item);
				}
				System.out.println("taxAuthorityList:"
						+ taxAuthorityList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return taxAuthorityList;
	}

	public void setTaxAuthorityList(List<SelectItem> taxAuthorityList) {
		this.taxAuthorityList = taxAuthorityList;
	}

	public List<SelectItem> getRateTypeList() {
		if (rateTypeList == null) {
			rateTypeList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			rateTypeList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("RATE-TYPE");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					rateTypeList.add(item);
				}
				System.out.println("rateTypeList:" + rateTypeList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return rateTypeList;
	}

	public void setRateTypeList(List<SelectItem> rateTypeList) {
		this.rateTypeList = rateTypeList;
	}

	public List<SelectItem> getParentRateList() {
		if (parentRateList == null) {
			parentRateList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			parentRateList.add(defitem);
			SearchRuleRatesRequest masterRequest = new SearchRuleRatesRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			try {
				SearchRuleRatesResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchRuleRate(masterRequest);
				for (OPSRuleRates master : masterResponse.getOPSRuleRates()) {
					if (!master.getRateId().equalsIgnoreCase(getTempParentFlag())) {
						SelectItem item = new SelectItem(master.getRateId(),
								master.getRateId());
						parentRateList.add(item);
					}

				}
			} catch (ServiceException e) {
				e.printStackTrace();
			}
		}
		return parentRateList;

	}

	public void setParentRateList(List<SelectItem> parentRateList) {
		this.parentRateList = parentRateList;
	}

	public String getTempParentFlag() {
		return tempParentFlag;
	}

	public void setTempParentFlag(String tempParentFlag) {
		this.tempParentFlag = tempParentFlag;
	}

}
